package com.orangelife.mobile.individual.activity;

import java.util.HashMap;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.common.sp.UserSp;
import com.curry.android.http.HttpUtil;
import com.curry.android.util.JSONHelper;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.orangelife.mobile.common.biz.JSONRequest;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.util.StringUtil;
import com.orangelife.mobile.util.ToastHelper;

@Deprecated
public class ChangePwdActivity extends OrangeLifeBaseActivity {

    /** 标题 */
    private TextView tvTitle, tvTitleRight;
    /** 密码文本 */
    private EditText etOldPwd, etNewPwd, etRepeatPwd;
    /** 按钮 */
    private Button btnDone;

    JSONRequest jsonRequest = JSONRequest.getInstance();

    private HashMap<String, Object> map;

    @Override
    protected void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        setContentView(R.layout.activity_change_password);
        initeView();
        findView();
    }

    /** 初始化控件 */
    private void initeView() {
        tvTitle = (TextView) findViewById(R.id.tv_title);
        tvTitleRight = (TextView) findViewById(R.id.tv_title_right);
        etOldPwd = (EditText) findViewById(R.id.etOldPwd);
        etNewPwd = (EditText) findViewById(R.id.etNewPwd);
        etRepeatPwd = (EditText) findViewById(R.id.etRepeatPwd);
        btnDone = (Button) findViewById(R.id.btnDone);
    }

    /** 为控件赋值或设置状态等 */
    private void findView() {
        tvTitle.setText("修改密码");
        tvTitleRight.setVisibility(View.GONE);
        btnDone.setOnClickListener(listener);
    }

    /** 监听器 */
    private OnClickListener listener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (!isConnected()) {
                return;
            }
            String oldPwd = etOldPwd.getText().toString();
            String newPwd = etNewPwd.getText().toString();
            String repeatPwd = etRepeatPwd.getText().toString();

            if (StringUtil.isContainBlank(oldPwd)) {
                ToastHelper.getInstance().displayToastWithQuickClose(
                        getResources().getString(R.string.oldpwd_null));
            } else if (StringUtil.isContainBlank(newPwd)) {
                ToastHelper.getInstance().displayToastWithQuickClose(
                        getResources().getString(R.string.newpwd_null));
            } else if (StringUtil.isContainBlank(repeatPwd)) {
                ToastHelper.getInstance().displayToastWithQuickClose(
                        getResources().getString(R.string.repeatpwd_null));
            } else if (!newPwd.equals(repeatPwd)) {
                ToastHelper.getInstance().displayToastWithQuickClose(
                        getResources().getString(
                                R.string.passwords_not_match));
            } else {
                try {
                    oldPwd = StringUtil.md5(oldPwd);
                    newPwd = StringUtil.md5(newPwd);
                    repeatPwd = StringUtil.md5(repeatPwd);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                map = new HashMap<>();
                map.put("memberID", UserSp.getLoginID());
                map.put("memberPsw", oldPwd);
                map.put("newPwd", newPwd);
                sendRequestForChangePwd(map);
            }
        }
    };


    /**
     * 接收处理线程返回的消息
     */
    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(android.os.Message msg) {
            closeDialog();
            switch (msg.what) {
                case 500:
                    HashMap<String, Object> map2 = (HashMap<String, Object>) JSONHelper
                            .jsonToMap(String.valueOf(msg.obj));
                    if (map2 != null) {
                        String errInfo2 = String.valueOf(map2
                                .get(Constant.RESULT_MESSAGE_KEY));
                        ToastHelper.getInstance().displayToastWithQuickClose(
                                errInfo2);
                    } else {
                        ToastHelper.getInstance().displayToastWithQuickClose(
                                msg.obj.toString());
                    }
                    break;
                case 1:
                    if (msg.obj != null) {
                        HashMap<String, Object> map = (HashMap<String, Object>) JSONHelper
                                .jsonToMap(String.valueOf(msg.obj));
                        if (map != null) {
                            int errCode = Integer.parseInt(map.get(
                                    Constant.RESULT_KEY).toString());
                            String errInfo = String.valueOf(map
                                    .get(Constant.RESULT_MESSAGE_KEY));
                            if (errCode == Constant.RESULT_SUCCESS) {
                                ToastHelper.getInstance()
                                        .displayToastWithQuickClose(
                                                getResources().getString(
                                                        R.string.modify_success));
                                finish();
                            } else {
                                ToastHelper.getInstance()
                                        .displayToastWithQuickClose(errInfo);
                            }

                        } else {
                            ToastHelper.getInstance()
                                    .displayToastWithQuickClose(
                                            getResources().getString(
                                                    R.string.modify_faild));
                        }
                    }

                    break;
                case 1011:
                    isLogin(dialog);
                    break;
                default:
                    break;

            }

        }

        ;
    };


    /**
     * 提交修改
     */
    private void sendRequestForChangePwd(HashMap<String, Object> hashMap) {
        showDialog(this);
        String url = Constant.URL_CHANGEPWD;
        jsonRequest.sendRequest(2, url, hashMap, handler, 1, HttpUtil.ACTION_JSON);
    }

}
